package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@SuppressWarnings("all")
@Service
public class CategoryServiceImpl implements CategoryService {

    //注入mapper对象
    @Autowired
    private CategoryMapper categoryMapper;

    //dishMapper对象
    @Autowired
    private DishMapper dishMapper;

    //注入setmealMapper对象
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        //开启分页查询（PageHelper）
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());

        //调用mapper对象进行分页查询
        Page<Category> page = categoryMapper.page(categoryPageQueryDTO.getName(),categoryPageQueryDTO.getType());

        //获取total,records
        long total = page.getTotal();
        List<Category> records = page.getResult();

        //封装PageResult对象并返回
        PageResult pageResult = PageResult.builder()
                .total(total)
                .records(records)
                .build();

        return pageResult;
    }

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    public void save(CategoryDTO categoryDTO) {
        //DTO转换为实体类
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);

        //设置分类状态,默认为0，禁用
        category.setStatus(StatusConstant.DISABLE);

        //设置创建时间和更新时间
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());

        //设置创建人
        category.setCreateUser(BaseContext.getCurrentId());

        //调用mapper对象进行新增
        categoryMapper.insert(category);
    }

    /**
     * 根据id删除分类
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //查询该分类下是否关联菜品
        int dishCount = dishMapper.countByCategoryId(id);
        if(dishCount > 0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //查询该分类下是否关联套餐
        int setmealCount = setmealMapper.countByCategoryId(id);
        if(setmealCount > 0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        //调用mapper对象进行删除
        categoryMapper.deleteById(id);
    }

    /**
     * 修改分类
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        //DTO转换为实体类
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);

        //设置修改时间和操作人
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());

        //调用mapper进行修改
        categoryMapper.update(category);
    }

    /**
     * 启用或禁用分类
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //创建category对象
        Category category = Category.builder()
                .status(status)
                .id(id)
                .build();

        //设置修改时间和修改人
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());

        //调用mapper对象进行修改
        categoryMapper.update(category);
    }

    /**
     * 根据type查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        //调用mapper对象查询
        return categoryMapper.list(type);
    }
}
